package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.findByCondition(queryString);
        List<Role> rows = page.getResult();
        long total = page.getTotal();
        return new PageResult(total, rows);
    }

    @Override
    public Map<String, Object> findAllDetail() {
        List<Menu> menulist = menuDao.findAll();
        List<Permission> authoritylist = permissionDao.findAll();
        Map<String, Object> map = new HashMap<>();
        map.put("menulist", menulist);
        map.put("authoritylist", authoritylist);
        return map;
    }

    @Override
    public void add(Role role, Integer[] authorityIds, Integer[] menuIds) {
        roleDao.add(role);
        Integer roleId = role.getId();
        setRoleAndMenu(menuIds, roleId);
        setRoleAndPermission(authorityIds, roleId);
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public Map<String, Object> findmenuIdsAndpermissionIdByroleId(Integer roleId) {
        List<Integer> menuIds = roleDao.findmenuByroleId(roleId);
        List<Integer> authorityIds = roleDao.findpermissionIdByroleId(roleId);
        Map<String, Object> map = new HashMap<>();
        map.put("menuIds", menuIds);
        map.put("authorityIds", authorityIds);
        return map;
    }

    @Override
    public void edit(Role role, Integer[] authorityIds, Integer[] menuIds) {
        Integer roleId = role.getId();
        roleDao.edit(role);
        List<Integer> menulist = roleDao.findmenuByroleId(roleId);
        List<Integer> permlist = roleDao.findpermissionIdByroleId(roleId);
        if (menulist != null && menulist.size() > 0) {
            roleDao.deleteMenu(roleId);
        }
        if (permlist != null && permlist.size() > 0) {
            roleDao.deletePerm(roleId);
        }
        setRoleAndMenu(menuIds, roleId);
        setRoleAndPermission(authorityIds, roleId);
    }

    @Override
    public void delete(Integer roleId) {
        roleDao.deleteMenu(roleId);
        roleDao.deletePerm(roleId);
        roleDao.deleteRole(roleId);
    }

    private void setRoleAndMenu(Integer[] menuIds, Integer roleId) {
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map<String, Integer> map = new HashMap<String, Integer>();
                map.put("roleId", roleId);
                map.put("menuId", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    private void setRoleAndPermission(Integer[] authorityIds, Integer roleId) {
        if (authorityIds != null && authorityIds.length > 0) {
            for (Integer permissionId : authorityIds) {
                Map<String, Integer> map = new HashMap<String, Integer>();
                map.put("roleId", roleId);
                map.put("permissionId", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }

    public List<Role> findAll() {
        return roleDao.findAll();
    }
}
